package com.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.reggie.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @program: reggie
 * 公共字段自动填充的处理器
 *
 * @author:
 * @create: 2022-09-28 11:34
 **/
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        long id = Thread.currentThread().getId();
        log.info("当前线程的id"+id);
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser", ThreadLocalUtil.getId());
        metaObject.setValue("updateUser",ThreadLocalUtil.getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",ThreadLocalUtil.getId());
        metaObject.setValue("updateUser",ThreadLocalUtil.getId());
    }
}
